#!/usr/bin/env bash

# curl -fsSL http://dev8.cn:8000/static/k8s/ubuntu-k8s1.sh | bash

# https://learnku.com/articles/29209
# https://www.cnblogs.com/fengyc/p/12392977.html

if [ `whoami` != "root" ];then
  echo "must be root"
  exit 1
fi

host=$1
if [ -z "$host" ]
then
  host='http://dev8.cn:8000/static'
fi

# 打开工具，磁盘，找到Swap分区，编辑挂载选项，取消选中 用户启动挂载，保存，重启即可。
sed -i 's/^GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX="ipv6.disable=1"/' /etc/default/grub
# update-grub
echo "disable ipv6 must reboot!"

###########################################################################

# apt-cache show kubeadm
# 设置环境变量, 命令行看着舒服点
export PS1="\[\033[33m\]\u\[\033[0m\]@\[\033[32m\]\W \[\033[0m\]$ "

# vim 显示行号
cat <<EOF > ~/.vimrc
set nu
EOF

ufw disable

#关闭虚拟内存
swapoff -a
# sed -i "s/\(^[^#].* swap .*$\)/#\1/" /etc/fstab

# 添加源
```
cat <<EOF >/etc/apt/sources.list.d/docker-k8s.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"insecure-registries": [
    "localhost:808"
  ],
"registry-mirrors":[
    "https://hub-mirror.c.163.com",
    "https://registry.aliyuncs.com",
    "http://7e61f7f9.m.daocloud.io",
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com",
    "https://reg-mirror.qiniu.com"
],
"graph":"/var/lib/docker"
}
EOF

systemctl restart docker
```

#加载所有的sysctl配置

    modprobe br_netfilter
    lsmod | grep br_netfilter

# 版本参考 https://hub.docker.com/r/mirrorgooglecontainers/kube-apiserver/tags


# 安装
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

apt-get update && apt-get install -y apt-transport-https
apt-get install -y kubeadm kubectl kubelet
systemctl start kubelet
systemctl enable kubelet

for  i  in  `kubeadm config images list`;  do
    imageName=${i#k8s.gcr.io/}
    docker pull registry.aliyuncs.com/google_containers/$imageName
    docker tag registry.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
    docker rmi registry.aliyuncs.com/google_containers/$imageName
done;

version=$(docker images | grep  k8s.gcr.io/kube-apiserver | tr -s ' ' | cut -d ' ' -f 2 | sort -r | head -n 1)
# kubeadm reset 如果重装。
kubeadm init --kubernetes-version=$version --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config


# weave 网络，让外部可以访问 kuboard,不用暴露端口。
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')

kubectl taint nodes --all node-role.kubernetes.io/master-



# 安装完毕，下面安装 kuboard
# https://kuboard.cn/install/install-dashboard.html

kubectl apply -f https://kuboard.cn/install-script/kuboard.yaml

# harbor
wget https://github.com/goharbor/harbor/releases/download/v1.10.4/harbor-online-installer-v1.10.4.tgz  -P /opt/docker -N
tar zxvf /opt/docker/harbor-online-installer-v1.10.4.tgz -C /opt/docker
wget $host/ops/harbor.yml -O /opt/docker/harbor/harbor.yml
/opt/docker/harbor/install.sh

# https://www.kuboard.cn/learning/k8s-intermediate/persistent/nfs.html
apt install -y nfs-server
mkdir -p /data/nfs
chmod +rw -R /data/nfs

# 目录 ip范围(读写,root,不做子目录检查)
echo "/data/nfs 192.168.3.0/24(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports

# 客户端执行
apt install nfs-common
mkdir -p /mnt/nfs-client
chmod +rw -R /mnt/nfs-client
mount -t nfs 192.168.3.30:/data/nfs /mnt/nfs-client
# 客户端执行 结束

showmount -e

# 查看token

echo "============================="
echo "安装完成!"
echo "k8s login token:"
kubectl -n kube-system get secret $(kubectl -n kube-system get secret | grep kuboard-user | awk '{print $1}') -o go-template='{{.data.token}}' | base64 -d

echo "如果重启，没有关闭交换分区，手动关闭: swapoff -a"
echo "查看kuboard service:kubectl get svc -n kube-system"
echo "kuboard url:"
echo "http://localhost:32567/login"

